package com.eedi.framework.oauth2.dal.mapper;

import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.oauth2.controller.sys.vo.token.SysOAuth2AccessTokenPageReq;
import com.eedi.framework.oauth2.dal.dataobject.SysOAuth2AccessTokenDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface SysOAuth2AccessTokenMapper extends BaseMapperX<SysOAuth2AccessTokenDO> {

    default SysOAuth2AccessTokenDO selectByAccessToken(String accessToken) {
        return selectOne(SysOAuth2AccessTokenDO::getAccessToken, accessToken);
    }

    default List<SysOAuth2AccessTokenDO> selectListByRefreshToken(String refreshToken) {
        return selectList(SysOAuth2AccessTokenDO::getRefreshToken, refreshToken);
    }

    default PageResult<SysOAuth2AccessTokenDO> selectPage(SysOAuth2AccessTokenPageReq reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysOAuth2AccessTokenDO>()
                .eqIfPresent(SysOAuth2AccessTokenDO::getSysUserId, reqVO.getSysUserId())
                .eqIfPresent(SysOAuth2AccessTokenDO::getSysUserType, reqVO.getSysUserType())
                .likeIfPresent(SysOAuth2AccessTokenDO::getSysOauth2ClientId, reqVO.getSystemOauth2ClientId())
                .gt(SysOAuth2AccessTokenDO::getExpiresTime, LocalDateTime.now())
                .orderByDesc(SysOAuth2AccessTokenDO::getSysOauth2AccessTokenId));
    }

    default List<SysOAuth2AccessTokenDO> selectByUserId(String userId, UserTypeEnum userType) {
        return selectList(new LambdaQueryWrapperX<SysOAuth2AccessTokenDO>()
                .eqIfPresent(SysOAuth2AccessTokenDO::getSysUserId, userId)
                .eqIfPresent(SysOAuth2AccessTokenDO::getSysUserType, userType));
    }

}
